using System;
using SharePoint.Security.ConfigAnalyzer.Engine.Base;
using SharePoint.Security.ConfigAnalyzer.Engine.Common;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using RuleInfo = SharePoint.Security.ConfigAnalyzer.Engine.Common.RuleInfo;

namespace SharePoint.Security.ConfigAnalyzer.Engine.Rules.SearchAdmin
{
    public class ContentAccessAccount : BaseRule
    {
        #region Overrides of BaseRule

        public ContentAccessAccount()
        {
            RuleInfo = new RuleInfo
                           {
                               Description = "Ensure that default content access account is not a member of farm administrators (Shared Services Administration: > Search Administration > Default content access account)",
                               Title = "Content Access Account",
                               ExpectedValue = "Account used should not be a Farm Admin"
                           };
        }

        public override void Validate()
        {
            string defaultGatheringAccount = string.Empty;

            foreach (SPService service in ObjectHelper.Farm.Services)
            {
                if (service is SearchService)
                {
                    var searchService = (SearchService)service;

                    foreach (SearchServiceApplication ssa in searchService.SearchApplications)
                    {

                        var content = new Content(ssa);
                        defaultGatheringAccount = content.DefaultGatheringAccount;
                        break;
                    }
                }
            }

            bool isFarmAdmin = false;

            SPSecurity.RunWithElevatedPrivileges(delegate()
                                                     {
                                                         SPGroup adminGroup = SPAdministrationWebApplication.Local.Sites[0].AllWebs[0].SiteGroups["Farm Administrators"];

                                                         foreach (SPUser user in adminGroup.Users)
                                                         {
                                                             if (user.LoginName.Equals(defaultGatheringAccount, StringComparison.InvariantCultureIgnoreCase))
                                                             {
                                                                 isFarmAdmin = true;
                                                                 break;
                                                             }
                                                         }
                                                     });

            if (isFarmAdmin)
            {
                Status= Status.Fail;
                CurrentValue = string.Format("Content access account [{0}] is a Farm Admin", defaultGatheringAccount);
            }
            else
            {
                Status = Status.Pass;
                CurrentValue = string.Format("Content access account [{0}] is not a Farm Admin", defaultGatheringAccount); ;
            }
            return ;
        }

        #endregion
    }
}